//电量获取相关；很多很多bug(电量获取异常)


String Batterystr="";
#include <Arduino.h>
#include "esp_adc_cal.h"
 float bv=0;
int BAT_PIN = 0;    // select the input pin for the ADC
 
#define ADC_EXAMPLE_CALI_SCHEME     ESP_ADC_CAL_VAL_EFUSE_TP
#define ADC_EXAMPLE_ATTEN           ADC_ATTEN_DB_11
 
static esp_adc_cal_characteristics_t adc1_chars;
bool cali_enable;
 
static bool adc_calibration_init(void)
{
    esp_err_t ret;
    bool cali_enable = false;
 
    ret = esp_adc_cal_check_efuse(ADC_EXAMPLE_CALI_SCHEME);
    if (ret == ESP_ERR_NOT_SUPPORTED) {
         Serial.println( "Calibration scheme not supported, skip software calibration");
    } else if (ret == ESP_ERR_INVALID_VERSION) {
        Serial.println("eFuse not burnt, skip software calibration");
    } else if (ret == ESP_OK) {
        cali_enable = true;
        esp_adc_cal_characterize(ADC_UNIT_1, ADC_EXAMPLE_ATTEN, ADC_WIDTH_BIT_12, 0, &adc1_chars);
    } else {
       Serial.println("Invalid arg");
    }
 
    return cali_enable;
}
 
 
 double getBatVolBfb(float batVcc);
double getV(String& Bstr) {
  pinMode(0, INPUT);
  cali_enable = adc_calibration_init();
  // read the value from the sensor:
  uint32_t sum = 0;
  analogSetAttenuation(ADC_11db);
  for (size_t i = 0; i < 50; i++)
  {
    sum += analogRead(BAT_PIN);
    delay(1);
  }
  sum /= 50;
  Serial.print("Channel Raw Data=  ");
  Serial.println(sum);
  if (cali_enable){
    uint32_t voltage0 = esp_adc_cal_raw_to_voltage(sum, &adc1_chars);
    
    double voltage=0.0261*voltage0+3.0;
    bv=voltage;
    Serial.print("Channel Votage=  ");
    Serial.println(voltage);
   double bfb=getBatVolBfb(voltage);
   Bstr=bfb;
  if(bfb==100)Bstr=Bstr.substring(0,3)+"%";
  else if(bfb<100&&bfb>=10) Bstr=Bstr.substring(0,2)+"%";
  else if(bfb<10)Bstr=Bstr.substring(0,3)+"%";
  return bfb;
  }
  //delay(2000);
}


double getBatVolNew(String& Bstr) //即时的电压
{
  //pinMode(17, OUTPUT);
  //digitalWrite(17, 1);
  delay(1);
  float vcc_cache = 0.0;
  for (uint8_t i = 0; i < 30; i++)
  {
    vcc_cache += analogRead(0) * 0.0009765625 * 5.607;
  }
  //digitalWrite(17, 0); //关闭电池测量
 // pinMode(17, INPUT);  //改为输入状态避免漏电
  String s=String(vcc_cache / 30);
  //bv=vcc_cache / 30;
  //centerx(s,50);
  Serial.print("电压：");
  Serial.println(vcc_cache / 30);
  Bstr=String(getBatVolBfb((vcc_cache / 30)));
  
  Bstr=getBatVolBfb((vcc_cache / 30));
  if(getBatVolBfb((vcc_cache / 30))==100)Bstr=Bstr.substring(0,3)+"%";
  else if(getBatVolBfb((vcc_cache / 30))<100&&getBatVolBfb((vcc_cache / 30))>=10) Bstr=Bstr.substring(0,2)+"%";
  else if(getBatVolBfb((vcc_cache / 30))<10)Bstr=Bstr.substring(0,3)+"%";

  return getBatVolBfb((vcc_cache / 30));
  
  
}
double getBatVolBfb(float batVcc) //获取电压的百分比，经过换算并非线性关系
{
  double bfb = 0.0;
  //y = 497.50976 x4 - 7,442.07254 x3 + 41,515.70648 x2 - 102,249.34377 x + 93,770.99821
  bfb = 497.50976 * batVcc * batVcc * batVcc * batVcc
        - 7442.07254 * batVcc * batVcc * batVcc
        + 41515.70648 * batVcc * batVcc
        - 102249.34377 * batVcc
        + 93770.99821;
  if (bfb > 100) bfb = 100.0;
  else if (bfb < 0) bfb = 3.0;
  return bfb;
}
